
package com.jf.cloud.multishop.task;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.multishop.constant.ShopWalletZoneConstant;
import com.jf.cloud.multishop.service.ShopDetailService;
import com.jf.cloud.multishop.service.ShopWalletLogService;
import com.jf.cloud.multishop.service.ShopWalletZoneService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 店铺定时任务
 *
 * @author zz
 * @Date 2021/7/9 13:35
 */
@Component
public class ShopTask {

    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private ShopWalletLogService shopWalletLogService;
    @Autowired
    private ShopWalletZoneService shopWalletZoneService;

    /**
     * 根据签约时间改变店铺状态
     */
    @XxlJob("changeShopStatus")
    public void changeShopStatus() {
        Date now = new Date();
        // 根据签约时间修改店铺状态
        shopDetailService.changeShopStatusByContractTime(now);
    }

    /**
     * 根据需要把店铺分成10片
     */
    @XxlJob("zoneShopWalletToTenIfNecessary")
    public void zoneShopWalletToTenIfNecessary() {
        List<Long> shopIds = shopWalletLogService.listNeedZoneShopIds();

        if (CollUtil.isEmpty(shopIds)) {
            return;
        }

        // 进行钱包分片
        for (Long shopId : shopIds) {
            shopWalletZoneService.doZone(shopId, 0);
        }

        // 移除缓存
        shopWalletZoneService.removeZoneCache(shopIds);
    }
}
